<?php

/**
* @filename 项目管理
*/
namespace app\admin\controller;

use app\admin\model\ProjectUser;
use think\Controller;
use app\admin\model\Project as Projects;
use app\admin\model\Tags;
use app\admin\model\Team;
use app\admin\model\Batchpay;
use app\admin\model\Order;
use app\admin\model\OrderContract;
use app\admin\model\ProjectAction;
use app\admin\model\Contract;
use app\admin\model\Member;
use expand\BestSignDemo;
use think\Db;
use app\index\model\YingMu;

use expand\Yunst;

class Project extends Common
{
    private $cModel;   //当前控制器关联模型
    
    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Projects;   //别名：避免与控制名冲突
    }
    //列表
    public function index()
    {
        $where = [];
        $search=input('get.search');
		$status=input('get.status');
		$project_type=input('get.project_type');
        if(!empty($search)){
            $where['p.project_name|p.target_code'] = ['like', '%'.$search.'%'];
        }

		if (isset($_GET['status']) && input('get.status') != -3){
			$where['p.status'] = ['eq',input('get.status')];
		}
		if (input('get.project_type')){
			$where['p.project_type'] = ['eq',input('get.project_type')];
		}
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = 'p.'.$order[0].' '.$order[1];
        }else{
            $order = 'p.id desc';
        }
		$field="p.*,pu.company_name";
        $dataList = $this->cModel
				->field($field)
				->alias('p')
				->join('tf_project_user pu','p.initiator=pu.id')->field($field)
				->where($where)->order($order)->paginate('', false, page_param());
		foreach($dataList as &$v){
			$v['have_money'] = $this->GetProjectBar($v['id']);
		}
		unset($v);
		$this->assign('PstatusList', Pstatus());
        $this->assign('dataList', $dataList);
		$this->assign('status', $status);
		$this->assign('project_type', $project_type);
        return $this->fetch();
    }
    //新建
    public function create()
    {
        if (request()->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                if($data['thumb']){
                    $data['thumb'] = rtrim($data['thumb'], ",");
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
                $id = $this->cModel->getLastInsID();
                // 提交事务
                if ($result && $id){
                    Db::commit();
					//同步影沐项目表
//					$ym_model = new YingMu();
//					$project_info = $this->cModel->where(['id'=>$id])->find();
//					$project_info =  empty($project_info)?array():$project_info->toArray();
//					$ym_model->insertYingMuProject($project_info);
                    return ajaxReturn(lang('action_success'), url('Project/index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }else{
			//项目方
//			$teModel = new Team();
//			$TeamList = $teModel->where(['status'=>1])->select();
			$puModel = new ProjectUser();
			$where['safemobile'] = 'YES';
			$where['status'] = '5';
			$where['id'] = ['not in',[1,9]];  //屏蔽阿纳巴里与华音
			$companyList = $puModel->where($where)->select();
			//标签
			$tModel = new Tags();
			$TagList = $tModel->select();
			
            $this->assign('data', $data);
//            $this->assign('TeamList', $TeamList);
			$this->assign('companyList', $companyList);
            $this->assign('TagList', $TagList);
            $this->assign('PstatusList', Pstatus());
            return $this->fetch('edit');
        }
    }
    //编辑
    public function edit($id)
    {
        if (request()->isPost()){
            $data = input('post.');
            if(!empty($data['thumb'])){
                $data['thumb'] = rtrim($data['thumb'], ",");
            }
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.'.$fv)->allowField(true)->save($data, $data['id']);
            }else{
				$data['status'] = 0;
                $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data, $data['id']);
            }
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
			//项目方
//			$teModel = new Team();
//			$TeamList = $teModel->where(['status'=>1])->select();
			$puModel = new ProjectUser();
			$companyList = $puModel->where(['safemobile'=>'YES','status'=>5])->select();
			//标签
			$tModel = new Tags();
			$TagList = $tModel->select();
			//参数
            $data = $this->cModel->get($id);
			if($data['status']>0){
				$this->error('该状态下无法编辑',url('project/index'));
			}
            $this->assign('data', $data);
//            $this->assign('TeamList', $TeamList);
			$this->assign('companyList', $companyList);
            $this->assign('TagList', $TagList);
            $this->assign('PstatusList', Pstatus());
            return $this->fetch();
        }
    }
    //删除
    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                if (!empty($id_arr)){
					$orderModel = new Order();
					$orderContractModel = new OrderContract();
                    foreach ($id_arr as $val){
						$data = $this->cModel->where('id='.$val)->find();
						if($data['status']>0){
							return ajaxReturn('该状态下无法删除');
						}
                        $this->cModel->where('id='.$val)->delete();
                        //关联表数据
						$orderModel->where('id='.$val)->delete();
						$orderContractModel->where('id='.$val)->delete();						
                    }
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn(lang('action_fail'));
                }
            }
        }
    }
	//审核
	public function examine(){
        $status = input('get.status',0);
		$where['p.status'] = $status;
		
        if (input('get.search')){
            $where['p.project_name|p.target_code'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = 'p.'.$order[0].' '.$order[1];
        }else{
            $order = 'p.id desc';
        }
//        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
		$field="p.*,pu.company_name";
		$dataList = $this->cModel
				->field($field)
				->alias('p')
				->join('tf_project_user pu','p.initiator=pu.id')->field($field)
				->where($where)->order($order)->paginate('', false, page_param());
//		$dataList->Team;
		$this->assign('PstatusList', Pstatus());
        $this->assign('dataList', $dataList);
        $this->assign('status', $status);
        return $this->fetch();
	}
	//审核、拒绝
	public function Pyes($id){
        if (request()->isPost()){
            $data = input('post.');
			$startTime = $this->cModel->where(['id'=>$id])->value('start_time');
			if($data['status'] == 0){
				return ajaxReturn("请选择审核结果");
			}
			if($data['status']==1 && $startTime<=time()){
				$data['status'] = 2;
				//同步更新影沐
//				$ym_model = new YingMu();
//				$project_info = $this->cModel->where(['id'=>$id])->find();
//				$project_info =  empty($project_info)?array():$project_info->toArray();
//				$ym_model->updateYingMuProject($project_info);
			}
            $result = $this->cModel->validate(CONTROLLER_NAME.'.agreeNotice')->allowField(true)->save($data, $data['id']);
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
			//项目方
//			$teModel = new Team();
//			$TeamList = $teModel->where(['status'=>1])->select();
			$puModel = new ProjectUser();
			$companyList = $puModel->where(['safemobile'=>'YES','status'=>5])->select();
			//标签
			$tModel = new Tags();
			$TagList = $tModel->select();
			//参数
            $data = $this->cModel->get($id);
            $this->assign('data', $data);
//            $this->assign('TeamList', $TeamList);
			$this->assign('companyList', $companyList);
            $this->assign('TagList', $TagList);
            $this->assign('PstatusList', Pstatus());
            return $this->fetch();
        }
	}
	
	//满标
	public function full(){
		
		if(input('status')){
			$where['p.status'] = ['eq',input('status')];
		}else{
			$where['p.status'] = ['in','3,4,5,6'];
		}

        if (input('get.search')){
            $where['p.project_name|p.target_code'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = 'p'.$order[0].' '.$order[1];
        }else{
            $order = 'p.id desc';
        }
//        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
		$field="p.*,pu.company_name";
		$dataList = $this->cModel
				->field($field)
				->alias('p')
				->join('tf_project_user pu','p.initiator=pu.id')->field($field)
				->where($where)->order($order)->paginate('', false, page_param());
		foreach($dataList as &$v){
			$v['have_money'] = $this->GetProjectBar($v['id']);
		}
		unset($v);
		$this->assign('PstatusList', Pstatus());
        $this->assign('dataList', $dataList);
        return $this->fetch();
	}
	//详情
	public function xiangqing($id){
		//项目方
//		$teModel = new Team();
//		$TeamList = $teModel->where(['status'=>1])->select();
		$puModel = new ProjectUser();
		$companyList = $puModel->where(['safemobile'=>'YES','status'=>5])->select();
		//标签
		$tModel = new Tags();
		$TagList = $tModel->select();
		
		//进度
		$acModel = new ProjectAction();
		$fangkuan = $acModel->where(['project_id'=>$id,'type'=>1])->find();
		$huankuan = $acModel->where(['project_id'=>$id,'type'=>2])->find();
		$duifu = $acModel->where(['project_id'=>$id,'type'=>3])->find();
		//参数
		$data = $this->cModel->get($id);
		$data['have_money'] = $this->GetProjectBar($id);
		$this->assign('data', $data);
//		$this->assign('TeamList', $TeamList);
		$this->assign('companyList', $companyList);
		$this->assign('fangkuan', $fangkuan);
		$this->assign('huankuan', $huankuan);
		$this->assign('duifu', $duifu);
		$this->assign('TagList', $TagList);
		$this->assign('PstatusList', Pstatus());
		return $this->fetch();
	}
	//放款
	public function fangkuang($id){
		if(request()->isPost()){
			$actionModel = new ProjectAction();
			$data = input('post.');
			if(empty($data['money'])){
				return ajaxReturn('金额不能为空');
			}
			$result = $actionModel->insert($data);
			if($result){
				$res = $this->cModel->where(['id'=>$id])->update(['status'=>4]);
				return ajaxReturn('操作成功',url('Project/full'));
			}else{
				return ajaxReturn($actionModel->getError());
			}
		}
		else{
			$this->assign('id',$id);
			$this->assign('type','1');
			$this->assign('selftitle','放款');
			return $this->fetch();
		}
	}
	//还款
	public function huankuan($id){
		if(request()->isPost()){
			$actionModel = new ProjectAction();
			$data = input('post.');
			if(empty($data['money'])){
				return ajaxReturn('金额不能为空');
			}
			$result = $actionModel->insert($data);
			if($result){
				$res = $this->cModel->where(['id'=>$id])->update(['status'=>5]);
				return ajaxReturn('操作成功',url('Project/full'));
			}else{
				return ajaxReturn($actionModel->getError());
			}
		}
		else{
			$this->assign('id',$id);
			$this->assign('type','2');
			$this->assign('selftitle','还款');
			return $this->fetch('fangkuang');
		}
	}
	//兑付
	public function duifu($id){
		if(request()->isPost()){
			$actionModel = new ProjectAction();
			$data = input('post.');
			if(empty($data['money'])){
				return ajaxReturn('金额不能为空');
			}
			$result = $actionModel->insert($data);
			if($result){
				$res = $this->cModel->where(['id'=>$id])->update(['status'=>6]);
				return ajaxReturn('操作成功',url('Project/full'));
			}else{
				return ajaxReturn($actionModel->getError());
			}
		}
		else{
			$this->assign('id',$id);
			$this->assign('type','3');
			$this->assign('selftitle','兑付');
			return $this->fetch('fangkuang');
		}
	}
	//返本金
	public function tomoney($id){
		$orderModel = new Order();
		$dataList = $orderModel
			->alias('a')
			->field('a.uid,sum(a.money) as amount,b.bizUserId')
			->join('tf_member b','a.uid=b.id','LEFT')
			->where(['a.project_id'=>$id,'a.status'=>1])
			->group('a.uid')
			->select();
		//如果支持订单为空
		if(empty($dataList)){
			$this->cModel->where(['id'=>$id])->update(['toMoney'=>1]);
			//$orderModel->where(['project_id'=>$id,'status'=>1])->update(['status'=>2]);
			return ajaxReturn('操作成功',url('Project/full'));
		}

		$YunClass = new Yunst();
		/*批次号*/
		$Olist['order'] = 'back' . str_pad($id,4,'0',STR_PAD_LEFT);
		$list = [];
		foreach($dataList as $value){
			$list[] = $this->getDaiList($id,$value['uid'],$value['amount'],$value['bizUserId']);
		}
		$Olist['list'] = $list;

		$res = $YunClass->batchPay($Olist);

		if($res['status'] == 'OK'){
			$this->cModel->where(['id'=>$id])->update(['toMoney'=>1]);
			return ajaxReturn('操作成功',url('Project/full'));
		}else{
			return ajaxReturn($res['message']);
		}
	}
	
	//格式化 代收订单
	private function getDaiList($id,$uid,$amount,$bizUserId){
		$dataValue = [];
		$orderModel = new Order();
		$batchModel = new Batchpay();
		$dataList = $orderModel->where(['project_id'=>$id,'uid'=>$uid,'status'=>1])->order('id asc')->limit(100)->select();
		$dataValue['bizOrderNo'] = 'HryDF'.$id.str_pad($uid,7,'0',STR_PAD_LEFT);
		$batchModel->insert(['bizOrderNo'=>$dataValue['bizOrderNo'],'uid'=>$uid,'amount'=>$amount,'project_id'=>$id]);
		$collectPayList = [];
		foreach($dataList as $key=>$val){
			$collectPayList[$key]['bizOrderNo'] = $val['order_sn'];
			$collectPayList[$key]['amount'] = $val['money']*100;
		}
		$dataValue['collectPayList'] = $collectPayList;
		$dataValue['bizUserId'] = $bizUserId;
		$dataValue['accountSetNo'] = '12985739202038';
		$dataValue['backUrl'] = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME').'/'.'backcall/batchpay.html';
		$dataValue['amount'] = $amount*100;
		$dataValue['fee'] = 0;
		
		return $dataValue;
	}
	
	//支持订单
	public function order(){
		$orModel = new Order();

		$where = [];
		if(isset($_GET['status']) && input('get.status')!=-1){
			$where['a.status'] = ['eq',input('get.status')];
		}
		if(input('get.project_id')){
			$where['a.project_id'] = ['eq',input('get.project_id')];
		}
		if(input('get.start_date')){
			$where['a.create_time'][] = ['gt',strtotime(input('get.start_date').' 00:00:00')];
		}
		if(input('get.end_date')){
			$where['a.create_time'][] = ['lt',strtotime(input('get.end_date').' 23:59:59')];
		}
        if (input('get.search')){
            $where['a.order_sn|a.money|b.username|b.mobile|c.project_name'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
		
		if(input('get.down')){
			$tableHeader = ['订单号','会员名称','手机号','支持金额','所属项目','状态','创建时间'];
			$dataList = $orModel->field('a.*,b.username,b.mobile,c.project_name')
					->alias('a')
					->join('tf_member b','a.uid=b.id','LEFT')
					->join('tf_project c','a.project_id=c.id','LEFT')
					->where($where)
					->order($order)
					->select();
			$data = [];
			foreach($dataList as $k=>$v){
				$data[$k]['order_sn'] =  $v['order_sn']; 
				$data[$k]['username'] =  $v['username']; 
				$data[$k]['mobile'] =  $v['mobile'].' '; 
				$data[$k]['money'] =  $v['money']; 
				$data[$k]['project_name'] =  $v['project_name'];
				if($v['status']==2){
				    $status='已成功';
                }elseif ($v['status']==1){
                    $status='进行中';
                }elseif($v['status']==0) {
                    $status='未成功';
                }else{
                    $status='其他状态';
                }
				$data[$k]['status'] =  $status;
				$data[$k]['create_time'] =  $v['create_time']; 
			}

			ToPHPEXECL($tableHeader,$data,'支持订单');
		}
		
		$dataList = $orModel->field('a.*,b.username,b.mobile,c.project_name')
							->alias('a')
							->join('tf_member b','a.uid=b.id','LEFT')
							->join('tf_project c','a.project_id=c.id','LEFT')
							->where($where)
							->order($order)
							->paginate('', false, page_param());			
		$ProjectList = $this->cModel->field('id,project_name')->select();
						
        $this->assign('ProjectList', $ProjectList);
        $this->assign('dataList', $dataList);
		return $this->fetch();
	}
    //订单代付
    public function daifu($id){
        $orderClass = new Order();
        $YunClass = new Yunst();
        $memberClass = new Member();
        $orderData = $orderClass->where(['id'=>$id])->find();


        $bizUserId = $memberClass->where(['id'=>$orderData['uid']])->value('bizUserId');
        $bizOrderNo = 'HryDF'.$id.str_pad($bizUserId,7,'0',STR_PAD_LEFT);
        $message = $YunClass->signalAgentPay2($bizOrderNo,$bizUserId,$orderData['order_sn'],$orderData['money']);
        if($message['status'] == 'error'){
            $orderClass->where(['id'=>$orderData['id']])->update(['payment_status'=>2,'payment_remark'=>$message['message']]);
            return ajaxReturn($message['message']);
        }
        if($message['status'] == 'OK'){
            $orderClass->where(['id'=>$orderData['id']])->update(['payment_status'=>1]);
            return ajaxReturn('代付成功');
        }
    }
	
	//标签管理
	public function biaoqian(){
		$tModel = new Tags();
		$dataList = $tModel->select();
		$this->assign('dataList',$dataList);
		return $this->fetch();
	}
	
	public function infotag(){
		if(request()->isAjax()){
			$tModel = new Tags();
			$type = input('type');
			if($type == 'add'){
			    $tag = trim(input('post.name'));
			    if(empty($tag))  return ajaxReturn('标签不能为空');
			    
				$res = $tModel->allowField(true)->save(['tag_name'=>$tag]);
			}
			if($type == 'del'){
				$id = input('post.id');
				$res = $tModel->where(['id'=>$id])->delete();
			}
			if($res){
				return ajaxReturn('操作成功',url('Project/biaoqian'));
			}else{
				return ajaxReturn($tModel->getError());
			}
		}
	}
	
	//项目进度
	
	private function GetProjectBar($id,$type=''){
		$orModel = new Order();
		$Bar = $orModel->where(['project_id'=>$id,'status'=>['gt',1],'type'=>1])->sum('money');
		$Bar = $Bar ? $Bar : 0;
		if(!$type){
			return $Bar;
		}
		$pModel = new Project();
		$Total = $pModel->where(['id'=>$id])->value('target_code');
		return round($Bar/$Total*100,2).'%';
	}


	//批量发送合同
	public function batchht(){
        if(request()->isAjax()){
            $proid = input('id');
            $orModel = new Order();
            $data=$orModel->where(['status'=>'2'])->where(['project_id'=>$proid])->select()->toArray();

            foreach ($data as $v){
                $userid=$v['uid'];
                $res=$this->makeContract($v['project_id'],$v['id'],$v['money'],$userid);
            }
            if($res){
                return ajaxReturn('发送成功',url('index'));
            }else{
                return ajaxReturn('发送失败');
            }
        }
    }

    private function makeContract($id,$order_id,$money,$userid){

        $ocModel = new OrderContract;
        $cotModel = new Contract();
        $ConList = $cotModel->where(['status'=>1,'project_id'=>$id])->select();

        if(empty($ConList)){
            return false;
        }
        foreach($ConList as $v){
            $ocontract = [
                'project_id' => $id,
                'cid' => $v['id'],
                'money' => $money,
                'user_id'	=> $userid,
                'title'	=> $v['title'],
                'order_id' => $order_id,
                'create_time' => date('Y-m-d H:i:s')
            ];
            
            $id = $ocModel->insertGetId($ocontract);

            $contractId = $this->AddYuansu($id,$userid);

            $ocModel->where(['id'=>$id])->update(['contract_id'=>$contractId]);
        }
        return true;
    }

    private function AddYuansu($id,$userid){
        $cotModel = new Contract();
        $mModel = new Member();
        $Best_data = new BestSignDemo();
        $ocModel = new OrderContract;
        $where = ['id'=>$id,'user_id'=>$userid];
        $OCinfo = $ocModel->where($where)->find();
        $Cinfo = $cotModel->where(['id'=>$OCinfo['cid']])->find();

        $MemberInfo = $mModel->where(['id'=>$userid])->find();
        //姓名
        $ADDdata[] = $Best_data->writeContent($Cinfo['npage'],$Cinfo['xname'],$Cinfo['yname'],$MemberInfo['username']);
        //身份证
        $ADDdata[] = $Best_data->writeContent($Cinfo['id_card'],$Cinfo['xcard'],$Cinfo['ycard'],$MemberInfo['certificate']);

        //金额
        $ADDdata[] = $Best_data->writeContent($Cinfo['mpage'],$Cinfo['xmoney'],$Cinfo['ymoney'],$OCinfo['money']);
        //大写金额
        $ADDdata[] = $Best_data->writeContent($Cinfo['hz_page'],$Cinfo['hz_x'],$Cinfo['hz_y'],num_to_rmb($OCinfo['money']));

        //金额2
        $ADDdata[] = $Best_data->writeContent($Cinfo['mtopage'],$Cinfo['xtomoney'],$Cinfo['ytomoney'],$OCinfo['money']);
        //大写金额2
        $ADDdata[] = $Best_data->writeContent($Cinfo['hzto_page'],$Cinfo['hzto_x'],$Cinfo['hzto_y'],num_to_rmb($OCinfo['money']));

        $res = $Best_data->putContent($Cinfo['fid'],$ADDdata);
        $contractId = $Best_data->create($res->data->fid,$Cinfo['title'],$Cinfo['expiretime']);
        return $contractId;
    }
    
    //轮播图列表
    public function carouselist() {
        $where['p.status'] = ['>', 0];
//        $dataList = $this->cModel->where($where)->order('id desc')->paginate('', false, page_param());
		$field="p.*,pu.company_name";
		$dataList = $this->cModel
				->field($field)
				->alias('p')
				->join('tf_project_user pu','p.initiator=pu.id')->field($field)
				->where($where)->order('id desc')->paginate('', false, page_param());
        $this->assign('PstatusList', Pstatus());
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }
    
    //轮播图管理(上传)
    public function carouselMap() {
        if($_POST){
            $row = Db::table('tf_project')->where('id',$_POST['id'])->update(['carousel_map'=>$_POST['picture']]);
            return ajaxReturn(lang('action_success'), url('carouselist'));
        }
        $id = request()->param('id');
        $this->assign('id', $id);
        return $this->fetch();
    }

	//更新项目授权信息
	public function authorizeProject()
	{
		if (request()->isAjax() && request()->isPost()) {
			$id = intval($_POST['pid']);
			if (Db::table('tf_project')->where(['id' => $id])->update(['has_authorize' => 2])) {
				ajaxReturn('授权成功', url('project/index'));
			} else {
				ajaxReturn('授权失败');
			}
		}
	}

	//授权项目详情页面
	public function authDetail($id)
	{
		$pid=$id;
		$model= new Projects();
		$order_model = new Order();
		$p_info = $model->where(['id'=>$pid])->find();
		$total_price = $order_model->field('sum(money) as total_price')->where(['type'=>1,'status'=>['>',1],'project_id'=>$pid])->find();
		$no_fang = $order_model->field('sum(money) as price')->where(['rights_state'=>1,'project_id'=>$pid,'type'=>1,'status'=>['>',1]])->find(); //未放弃
		$has_fang = $order_model->field('sum(money) as price')->where(['rights_state'=>['gt',1],'project_id'=>$pid,'type'=>1,'status'=>['>',1]])->find(); //已放弃
		$this->assign('p_info', $p_info);
		$this->assign('total_price',empty($total_price['total_price']) ? 0: $total_price['total_price']);
		$this->assign('no_fang',empty($no_fang['price']) ? 0 :$no_fang['price']);
		$this->assign('has_fang',empty($has_fang['price']) ? 0 :$has_fang['price']);
		return $this->fetch();
	}

	//项目以放弃与未放弃名单页面
	public  function projectMemberList($id,$type){
		$pid=$id;
		$model= new Projects();
		$order_model = new Order();
		$p_info = $model->where(['id'=>$pid])->find();
		if(intval($type)===1){
			$fang_price = $order_model->field('sum(money) as price')->where(['rights_state'=>1,'project_id'=>$pid,'type'=>1,'status'=>['>',1]])->find(); //未放弃
		}else{
			$fang_price = $order_model->field('sum(money) as price')->where(['rights_state'=>['gt',1],'project_id'=>$pid,'type'=>1,'status'=>['>',1]])->find(); //已放弃
		}
		$filed = 'm.id as uid ,m.mobile,m.username,sum(p.money) as money,p.rights_state,p.rights_time';
		if(intval($type)===1) {
			$lists =$order_model->alias('p')->join('member m','m.id=p.uid')->where(['p.project_id'=>intval($pid),'p.type'=>1,'p.status'=>['>',1],'p.rights_state'=>1])->field($filed)->group('p.uid')->paginate('', false, page_param());

		}else{
			$lists =$order_model->alias('p')->join('member m','m.id=p.uid')->where(['p.project_id'=>intval($pid),'p.type'=>1,'p.status'=>['>',1],'p.rights_state'=>['gt',1]])->field($filed)->group('p.uid')->paginate('', false, page_param());

		}

		if(input('get.down')){
			$filed = 'm.id as uid ,m.mobile,m.username,sum(p.money) as money,p.rights_state,p.rights_time';
			if(intval($type)===1){
				$lists =$order_model->alias('p')->join('member m','m.id=p.uid')->where(['p.project_id'=>intval($pid),'p.type'=>1,'p.status'=>['>',1],'p.rights_state'=>1])->field($filed)->group('p.uid')->select();

			}else{
				$lists =$order_model->alias('p')->join('member m','m.id=p.uid')->where(['p.project_id'=>intval($pid),'p.type'=>1,'p.status'=>['>',1],'p.rights_state'=>['gt',1]])->field($filed)->group('p.uid')->select();

			}
			$tableHeader = ['姓名','手机号','认投金额','放弃权益','放弃时间','放弃至项目方'];
			$data = [];
			foreach($lists as $k=>$v){
				$data[$k]['username'] =  $v['username'];
				$data[$k]['mobile'] =  $v['mobile'].' ';
				$data[$k]['money'] =  $v['money'];
				if($v['rights_state']>1){
					$data[$k]['auth_name'] ='是' ;
				}else{
					$data[$k]['auth_name'] = '否';
				}
				$data[$k]['create_time'] =  $v['rights_time'];
				if($v['rights_state']==2){
					$data[$k]['confirm_project'] = '拼车车';
				}elseif($v['rights_state']==3){
					$data[$k]['confirm_project'] = '达客云';
				}else{
					$data[$k]['confirm_project'] = '';
				}
			}
			if(intval($type)>1){
				ToPHPEXECL($tableHeader,$data,'已放弃名单');
			}else{
				ToPHPEXECL($tableHeader,$data,'未放弃名单');
			}

		}else{
			$this->assign('p_info', $p_info);
			$this->assign('type', intval($type));
			$this->assign('fang_price', empty($fang_price['price']) ? 0 :$fang_price['price']);
			$this->assign('dataList', $lists);
			return $this->fetch();
		}



	}


	//项目以放弃与未放弃名单页面
	public  function hasGiveUpOrderList($id,$type,$uid){
		$pid=$id;
		$model= new Projects();
		$order_model = new Order();
		$m_model = new Member();
		$user_info = $m_model->where(['id'=>intval($uid)])->find();
		$p_info = $model->where(['id'=>intval($pid)])->find();
		$order_count = $order_model->where(['uid'=>intval($uid),'project_id'=>intval($pid),'rights_state'=>intval($type)])->count();
		$filed =  'm.mobile,m.username,p.money,p.rights_state,p.rights_time,p.from_user_id,m1.username as from_username,m1.mobile as from_mobile';
		$lists =$order_model->alias('p')
				->join('member m','m.id=p.uid')
				->join('member m1','m1.id=p.from_user_id','left')
				->where(['p.project_id'=>intval($pid),'p.type'=>1,'p.uid'=>intval($uid),'p.status'=>['>',1],'p.rights_state'=>intval($type)])
				->field($filed)
				->paginate('', false, page_param());

		if(input('get.down')){
			$filed = 'm.mobile,m.username,p.money,p.rights_state,p.rights_time,p.from_user_id,m1.username as from_username,m1.mobile as from_mobile';
			$lists =$order_model->alias('p')
					->join('member m','m.id=p.uid')
					->join('member m1','m1.id=p.from_user_id','left')
					->where(['p.project_id'=>intval($pid),'p.uid'=>intval($uid),'p.type'=>1,'p.status'=>['>',1],'p.rights_state'=>intval($type)])
					->field($filed)
					->select();
			$tableHeader = ['手机号','认投金额','放弃权益','放弃时间','类型','对象姓名','对象电话'];
			$data = [];
			foreach($lists as $k=>$v){
				$data[$k]['mobile'] =  $v['mobile'].' ';
				$data[$k]['money'] =  $v['money'];
				if($v['rights_state']>1){
					$data[$k]['auth_name'] ='是' ;
				}else{
					$data[$k]['auth_name'] = '否';
				}
				$data[$k]['create_time'] =  $v['rights_time'];
				if(!empty($v['from_user_id'])){
					$data[$k]['type'] = '收入';
					$data[$k]['from_username'] =  $v['from_username'];
					$data[$k]['from_mobile'] =  $v['from_mobile'];
				}else{
					$data[$k]['type'] = '';
					$data[$k]['from_username'] ='';
					$data[$k]['from_mobile'] ='';
				}
			}
			if(intval($type)>1){
				ToPHPEXECL($tableHeader,$data,'已放弃订单');
			}else{
				ToPHPEXECL($tableHeader,$data,'未放弃订单');
			}

		}else{
			$this->assign('user_info',$user_info);
			$this->assign('uid',intval($uid));
			$this->assign('p_info', $p_info);
			$this->assign('type', intval($type));
			$this->assign('order_count',empty($order_count) ? 0 :$order_count);
			$this->assign('dataList', $lists);
			return $this->fetch();
		}

	}








    
}